package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public ResultModel login(UserModel model) {
        ResultModel resultModel = new ResultModel();
        if (!model.getSessionCode().equals(model.getCode())) {
            resultModel.setMsg("error-code");
            return resultModel;
        }
        UserDao userDao = new UserImpl();
        UserModel userModel = userDao.findByUsernameAndPassword(model);
        boolean isHave = userModel != null;
        if (!isHave) {
        } else {
            resultModel.setMsg("success");
            return resultModel;
        }
        resultModel.setMsg("error");
        return resultModel;
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        List<UserModel> list = userDao.findAll(userModel);
        resultModel.setData(list);
        return resultModel;
    }

    @Override
    public ResultModel add(UserModel model) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.add(model);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel findById(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        UserModel model = userDao.findById(userModel);
        resultModel.setData(model);
        return resultModel;
    }

    @Override
    public ResultModel update(UserModel model) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.update(model);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel delete(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.delete(userModel);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel passwordEdit(String uid, String old_password, String new_password) {
        ResultModel resultModel = new ResultModel();
        UserModel userModel = new UserModel();
        userModel.setId(Integer.parseInt(uid));
        UserDao userDao = new UserImpl();
        UserModel model = userDao.findById(userModel);
        String password = model.getPassword();
        if (password.equals(old_password) ) {
            int count = userDao.updatePassword(uid,new_password);
            if (count > 0) {
                resultModel.setMsg("success");
            } else {
                resultModel.setMsg("error");
            }
        } else {
            resultModel.setMsg("error-password");
        }
        return resultModel;
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.getCount(userModel);
        resultModel.setCount(count);
        return resultModel;
    }
}
